<?php
/**
 * Created by PhpStorm.
 * User: yxs
 * Date: 2017/7/3
 * Time: 11:46
 */

namespace app\cart\controller;

use icontroller\wechat\AController;

use model\ShopUserCart;
use model\ShopGoods;
use model\ShopStore;
use model\ShopGoodsVariant;

class Cart extends AController
{

    // <editor-fold defaultstate="collapsed" desc="获取购物车数据">
    public function index()
    {
        $sc = ShopUserCart::all(["account_id" => $this->account_id]);
        $view_data = [];
        $goods_item = [];
        $store_total_price = [];
        $total_price = 0;
        $buy = [];
        $st_enable = [];
        foreach ($sc as $v) {
            $good = $v->shopGoods;
            // print_r($v->goods_id);

            $good["num"] = $v["quantity"];
            //  if ($good->flag_delete != 1 && $good->flag_putaway != 0) {
            $ss = ShopStore::get($good->store_id);
            if (!isset($st_enable[$ss["id"]])) {
                $st_enable[$ss["id"]] = 0;
            }

            $good->enable = 1;
            if ($v->variant_id > 0) {
                $variant = ShopGoodsVariant::get($v->variant_id);
                if ($variant) {
                    $good->price = $variant->price;
                    $good->name = $good->name . "(" . $variant->name . ")";
                }
                if ($variant->flag_delete == 1) {
                    $v->flag_choose = false;
                    $good->enable = 0;
                }
            }

            if ($good->flag_delete == 1 || $good->flag_putaway == 0) {
                $good->enable = 0;
                $v->flag_choose = false;
            }
            if ($good->enable == 1) {
                $st_enable[$ss["id"]] = 1;
            }
            $good->cart_id = $v->id;
            $good->cart_variant_id = $v->variant_id;
            $good->flag_choose = $v->flag_choose;
            if ($v->flag_choose == 1 && $good->enable == 1) {
                $t_price = bcmul($good->price, $v["quantity"], 2);
                $buy[] = $v->id;
            } else {
                $t_price = 0;
            }
            $goods_item[$ss["id"]][] = $good;
            $total_price += $t_price;
            $total_price = number_format($total_price, 2, '.', '');

            if (key_exists($good->store_id, $store_total_price)) {
                $store_total_price[$good->store_id] += $t_price;
            } else {
                $store_total_price[$good->store_id] = $t_price;
            }
            $store_total_price[$ss["id"]] = number_format($store_total_price[$ss["id"]], 2, '.', '');
            $view_data[$ss["id"]] = [
                "id" => $ss["id"],
                "name" => $ss["name"],
                "logo" => $ss["logo"],
                "enable" => $st_enable[$ss["id"]],
                "check" => $store_total_price[$good->store_id] > 0 ? true : false,
                "total_price" => $store_total_price[$good->store_id],
                "items" => $goods_item[$ss["id"]]
            ];
            //  }
        }

        if (empty($buy)) {
            $this->assign("buy", 0);
        } else {
            $this->assign("buy", implode(",", $buy));
        }

        $this->assign("total_price", $total_price);
        $this->assign("list", $view_data);
        return view();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="购物车删除记录">
    public function delete()
    {
        $cart_id = input("cart_id");
        $item = ShopUserCart::get(["account_id" => $this->account_id, "id" => $cart_id]);
        if ($item) {
            $item->delete();
        }
        $this->success("删除成功");
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="购物车打勾">
    public function check()
    {
        $str_cart_id = input("cart_id");

        $arr_cart_id = explode(",", $str_cart_id);
        $data = ShopUserCart::all(["id" => ["in", $arr_cart_id]]);
        $price = 0;
        foreach ($data as $v) {
            if ($v->variant_id > 0) {
                $p = ShopGoodsVariant::get($v->variant_id);

            } else {
                $p = ShopGoods::get($v->goods_id);
            }
            if ($p) {
                $price += bcmul($p->price, $v->quantity, 2);
            }

            $v->save(["flag_choose" => 1]);
        }

        $this->success("设置成功", null, ["price" => number_format($price, 2, '.', '')]);
//        $result = ShopUserCart::update(["flag_choose"=>1], ["id" =>["in",$str_cart_id] ]);
//        if ($result) {
//            $this->success("设置成功",null,["price"=>$price]);
//        } else {
//            $this->success("设置出错");
//        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="购物车取消打勾">
    public function un_check()
    {
        $str_cart_id = input("cart_id");
        $arr_cart_id = explode(",", $str_cart_id);
        $data = ShopUserCart::all(["id" => ["in", $arr_cart_id]]);
        $price = 0;
        foreach ($data as $v) {
            if ($v->variant_id > 0) {
                $p = ShopGoodsVariant::get($v->variant_id);
            } else {
                $p = ShopGoods::get($v->goods_id);
            }
            if ($p) {
                $price += bcmul($p->price, $v->quantity,2);
            }
            $v->save(["flag_choose" => 0]);
        }
        $this->success("设置成功", null, ["price" => number_format($price, 2, '.', '')]);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="购物车是否有商品">
    public function have_cart()
    {
        $store_id = input('store_id');
        $ids = [];
        $result = ShopUserCart::all(["account_id" => $this->account_id]);
        foreach ($result as $v) {
            if ($v->shopGoods->flag_putaway == 1 && $v->shopGoods->flag_delete != 1 && $v->shopGoods->store_id == $store_id) {
                if ($v->variant_id > 0) {
                    $variant = ShopGoodsVariant::get($v->variant_id);
                    if ($variant && $variant->flag_delete == 0) {
                        $ids[] = $v->id;
                    }
                } else {
                    $ids[] = $v->id;
                }
            }
        }

        if (!empty($ids)) {
            $this->success("已加入购物车", null, implode(",", $ids));
        } else {
            $this->error("未有商品加入购物车");
        }
        return view("settle");
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="添加商品入购物车">
    public function add_cart()
    {
        $variant_id = input('variant_id', 0);
        $type = input('type');
        $data = [
            "account_id" => $this->account_id,
            "goods_id" => input('goods_id'),
            "variant_id" => $variant_id
        ];

        $good = ShopGoods::get($data["goods_id"]);
        $gv = new ShopGoodsVariant();
        if ($good->flag_delete != 1 && $good->flag_putaway != 0) {
            $price = $good->price;
            if (!empty($variant_id)) {
                $price = $gv->where(["id" => $variant_id, "flag_delete" => 0])->value("price");
                if (empty($price)) {
                    $this->error("此规格商品已下架");
                }
            }
            if ($type == "del" && empty($variant_id)) {
                $sc_mo = new ShopUserCart();
                $list = $sc_mo->where(["goods_id" => $data["goods_id"], "account_id" => $this->account_id])->select();

                $count = count($list);
                if ($count > 1) {
                    $this->error("多规格商品需要在购物车删除哦");
                }
                if ($count == 1) {
                    $variant_id = $list[0]->variant_id;
                    if ($list[0]->variant_id > 0) {
                        $price = $gv->where(["id" => $list[0]->variant_id])->value("price");
                    }
                }
            }
            $where = ["goods_id" => $data["goods_id"], "account_id" => $this->account_id, "variant_id" => $variant_id];
            $sc = ShopUserCart::get($where);

            if ($sc) {
                if ($type == "del") {
                    if ($sc["quantity"] < 2) {
                        $result = $sc->where($where)->delete();
                    } else {
                        $result = $sc->where($where)->setDec("quantity");
                    }
                } else {
                    $result = $sc->where($where)->setInc("quantity");
                }
            } else {
                $data["cart_price"] = $price;
                $data["quantity"] = 1;
                $sc_mo = new ShopUserCart();
                $result = $sc_mo->save($data);
            }
            if ($result) {
                $this->success("操作成功", null, ["price" => $price]);
            }
        } else {
            $this->error("此商品已下架");
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="获取购物车数据">
    public function cart_info()
    {
        $sc = ShopUserCart::all(["account_id" => $this->account_id]);
        $this->success("获取成功", "", $sc);
    }
    // </editor-fold>
}